package com.simas.screen;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
//import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
//import net.rim.device.api.ui.component.RichTextField;
//import net.rim.device.api.ui.component.NullField;
//import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.GridFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

import com.simas.common.CommonFunction;
import com.simas.common.CommonMenu;
import com.simas.common.CustomResources;
import com.simas.common.DeletePolisInfo;
//import com.simas.common.DeletePolisInfo;
import com.simas.common.XMLWaitProcess;
//import com.simas.common.OpenInfoScreen;
//import com.simas.common.OpenPolisScreen;
import com.simas.hid.CallHIDServlet;
import com.simas.component.CellButton;
import com.simas.component.CustomButton;
import com.simas.component.RowButton;

public class XmlHIDScreen extends MainScreen implements FieldChangeListener {
//	private CustomButton btViewPolis;
//	private CustomButton btViewKlaim;
	private CustomButton btNextKlaim;
	private CustomButton btPrevKlaim;
	private CellButton btHome;
	private CellButton btCInfoPolis;
	private CellButton btCInfoKlaim;
	private CellButton btReset;
	private LabelField lblSeparator1;
	private LabelField lblSeparator2;
	private LabelField lblSeparator3;

	private RowButton origBtPlan;
	
	CustomResources cr = new CustomResources();
	private int intIdxScreen = cr.getIntIdxHIDScreen();
	
	CommonFunction cf = new CommonFunction();

	private VerticalFieldManager vfm = new VerticalFieldManager(Field.FIELD_LEFT);
	private VerticalFieldManager vfmBenefit = new VerticalFieldManager(Field.FIELD_LEFT);
	private VerticalFieldManager vfmDiagnosis = new VerticalFieldManager(Field.FIELD_LEFT);
	private VerticalFieldManager vfmMaster;
	
	int intRows = 7;
	int intCols = 3;
	GridFieldManager gfmOrig = new GridFieldManager(intRows,intCols,Field.USE_ALL_WIDTH);
	
	int intKlaimRow = 15;
	int intKlaimCols = 3;
	GridFieldManager gfmKlaim = new GridFieldManager(intKlaimRow,intKlaimCols,Field.USE_ALL_WIDTH);
	GridFieldManager gfmKlaim1 = new GridFieldManager(intKlaimRow,intKlaimCols,Field.USE_ALL_WIDTH);
	
//	HorizontalFieldManager hfmScreen = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR);
	HorizontalFieldManager hfmBenefit;
	
	private String strHidNoKartu;
	private String strHidNoPolis;
	private String strHidNoReg;
	private String strHidTglLahir;
	private String strRootName;
	private String strPlanName;
	
	private int intCekPlan = 0;
	private int intCekBenefit = 0;
	private int intBenefitNo = 1;
	private int intCekKlaim = 0;
	
	private String strMaxNo;
	private String strRecNo;
	
	private static final int _tab = 4;
	
	/**
     * Menambah Menu Close
     */
    protected void makeMenu( Menu menu, int instance ) {
		CommonMenu cm = new CommonMenu(this);
		menu.add(cm._welcomeScreen);
	}
	   
    /**
     * Action utk Back Button
     */
    protected boolean keyDown(int keycode, int status)  {
		if(Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
			CommonMenu cm = new CommonMenu(this);
			cm.welcomeScreen();
			return true;
		}
		return false;
	}
	
    /**
     * Action Listener
     */
	public void fieldChanged(Field field, int context) {
		if (field == btHome) {
			CommonMenu cm = new CommonMenu(this);
			cm.welcomeScreen();
//		} else if (field == btViewPolis) {
//			fnViewPolisScreen();
//		} else if (field == btViewKlaim) {
//			fnViewKlaimScreen();
		} else if (field == btCInfoPolis) {
			fnViewPolisScreen();
		} else if (field == btCInfoKlaim) {
			fnViewKlaimScreen();
		} else if (field == btReset) {
			DeletePolisInfo dpi = new DeletePolisInfo();
			dpi.fnDeletePolis(this, intIdxScreen);
		}
//		if (field == INFOButton) {
//			//fnOpenInfoScreen();
//			OpenInfoScreen ois = new OpenInfoScreen();
//			ois.fnOpenInfoScreen(this);
//		} else if (field == POLISButton) {
//			//fnOpenPolisScreen();
//			OpenPolisScreen ops = new OpenPolisScreen();
//			ops.fnOpenPolisScreen(this);
//		} else if (field == RSTButton) {
//			//fnResetInfo();
//			DeletePolisInfo dpi = new DeletePolisInfo();
//			dpi.fnDeletePolis(this, intIdxScreen);
////			OpenLoginScreen ols = new OpenLoginScreen();
////			ols.fnOpenLoginScreen(this, intIdxScreen);
//		} else if (field == btViewPolis) {
//			//fnViewPolisScreen();
//			CallHIDServlet chs = new CallHIDServlet();
//			chs.fnCallHIDServlet2(this, strHidNoKartu, strHidNoPolis, strHidNoReg, strHidTglLahir);
//		} else if (field == btViewKlaim) {
//			fnViewKlaimScreen();
//		}
	}
	
	private void fnViewKlaimScreen() {
		CallHIDServlet chs = new CallHIDServlet();
		chs.fnCallHIDServlet5(this, strHidNoKartu, strHidNoPolis, strHidNoReg, strHidTglLahir, strRecNo, strMaxNo);
	}
	
	private void fnViewPolisScreen() {
		CallHIDServlet chs = new CallHIDServlet();
		chs.fnCallHIDServlet2(this, strHidNoKartu, strHidNoPolis, strHidNoReg, strHidTglLahir);
	}
	
	public XmlHIDScreen(String strURL) {
//		Bitmap resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("bgSimasSehat.png"));
//		Background bg = BackgroundFactory.createBitmapBackground(resizeBitmap, 
//																 Background.POSITION_X_LEFT, 
//																 Background.POSITION_Y_TOP, 
//																 Background.REPEAT_NONE);
//
//		vfmMaster = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT);
//		vfmMaster.setBackground(bg);
//		vfmMaster.setFont(cr.getCustomFont(8));
//		vfmMaster.add(new LabelField(" ", Field.USE_ALL_WIDTH));
//		vfmMaster.add(new LabelField(" ", Field.USE_ALL_WIDTH));
//		
//		// btHome
//		btHome = new CellButton("", Integer.parseInt("FFFF00", 16), 
//			 Integer.parseInt("FF0000", 16), 
//			 0, null, 0, 25, 50, 45);
//		btHome.setChangeListener(this);
//		
//		HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.FIELD_LEFT);
//		hfmButton.add(btHome);
//		vfmMaster.add(hfmButton);
//		
//		add(vfmMaster);
		
		Bitmap resizeBitmap = cf.getScaledBitmapImage(cr.getImageURL("bgSimasSehat.png"));
		Background bg = BackgroundFactory.createBitmapBackground(resizeBitmap, 
																 Background.POSITION_X_LEFT, 
																 Background.POSITION_Y_TOP, 
																 Background.REPEAT_NONE);

		vfmMaster = new VerticalFieldManager(Field.USE_ALL_WIDTH);
		vfmMaster.setBackground(bg);
		vfmMaster.setFont(cr.getCustomFont(4));
		for (int a=1; a<=7; a++) {
			vfmMaster.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		}
		
		btHome = new CellButton("Home", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btHome.setFont(cr.getCustomFont());
		btHome.setChangeListener(this);
		
		btCInfoPolis = new CellButton("Info Polis", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btCInfoPolis.setFont(cr.getCustomFont());
		btCInfoPolis.setChangeListener(this);
		
		btCInfoKlaim = new CellButton("Info Klaim", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btCInfoKlaim.setFont(cr.getCustomFont());
		btCInfoKlaim.setChangeListener(this);
		
		btReset = new CellButton("Reset", Integer.parseInt("000000", 16), 
				 Integer.parseInt("FFCC33", 16), 
				 0, null, 0, 0, 0, 0);
		btReset.setFont(cr.getCustomFont());
		btReset.setChangeListener(this);
		
		lblSeparator1 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator1.setFont(cr.getCustomFont());
		
		lblSeparator2 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator2.setFont(cr.getCustomFont());
		
		lblSeparator3 = new LabelField(" | ", Field.FIELD_LEFT);
		lblSeparator3.setFont(cr.getCustomFont());
		
		HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.FIELD_HCENTER);
		hfmButton.add(btHome);
		hfmButton.add(lblSeparator1);
		hfmButton.add(btCInfoPolis);
		hfmButton.add(lblSeparator2);
		hfmButton.add(btCInfoKlaim);
		hfmButton.add(lblSeparator3);
		hfmButton.add(btReset);
		
		vfmMaster.add(hfmButton);
		add(vfmMaster);
		
		gfmOrig.setFont(cr.getCustomFont());
		gfmKlaim.setFont(cr.getCustomFont());
		vfmDiagnosis.setFont(cr.getCustomFont());
		gfmKlaim1.setFont(cr.getCustomFont());
		
		XMLWaitProcess xmlwp = new XMLWaitProcess();
		xmlwp.WaitProcess(strURL,"GET", this);
	}
	
	/**
	 * request gagal 
	 */
	public void requestFailed(final String message) {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				Dialog.alert("Request failed. Reason: " + message);
			}
		});
	}
	
	/**
	 * request berhasil
	 */
	public void requestSucceeded(byte[] result, String contentType) {
		if (contentType.startsWith("text/")) {
			synchronized (UiApplication.getEventLock()) {
				
				String strResult = new String(result);
				try {
					DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		            DocumentBuilder builder = factory.newDocumentBuilder();
		            InputStream inputStream = new ByteArrayInputStream(strResult.getBytes());
		            Document document = builder.parse( inputStream );
		            Element rootElement = document.getDocumentElement();
		            rootElement.normalize();
		            
		            vfmMaster.add(new LabelField(" ", Field.USE_ALL_WIDTH));
		            
		    		displayNode( rootElement, 0 );
		            
		            // header data peserta
		    		HorizontalFieldManager hfmPeserta = cr.getHeaderRibbon("DATA PESERTA", 0);
        		    vfmMaster.add(hfmPeserta);
        		    
        		    // data peserta
        		    vfmMaster.add(gfmOrig);
        		    
        		    // tombol view klaim dan view polis
//        		    btViewPolis = new CustomButton("Lihat Polis", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
//        		    btViewPolis.setFont(cr.getCustomFont());
//		            btViewPolis.setChangeListener(this);
//		            btViewKlaim = new CustomButton("Lihat Klaim", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
//		            btViewKlaim.setFont(cr.getCustomFont());
//		    		btViewKlaim.setChangeListener(this);
//		    		HorizontalFieldManager buttonManager1 = new HorizontalFieldManager(Field.FIELD_LEFT);
//		    		buttonManager1.add(btViewPolis);
//		    		buttonManager1.add(btViewKlaim);
//		    		HorizontalFieldManager hfmButton = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
//        	            public void paint(Graphics graphics){
//        	                graphics.setBackgroundColor(Integer.parseInt("b3b3b3", 16));
//        	                graphics.clear();
//        	                super.paint(graphics);
//        	            }
//        	        };
//        	        hfmButton.add(buttonManager1);
//		    		vfmMaster.add(hfmButton);
		    		
		    		// plan
		    		if (intCekPlan == 1) {
		    			HorizontalFieldManager hfmPlan = cr.getHeaderRibbon("DAFTAR PLAN", 0);
	        		    vfmMaster.add(hfmPlan);
		    			vfmMaster.add(vfm);
		    			
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
		    		}
		    		
		    		// benefit
		    		if (intCekBenefit == 1) {
		    			HorizontalFieldManager hfmBenefit = cr.getHeaderRibbon("DAFTAR BENEFIT - " + strPlanName, 0);
	        		    vfmMaster.add(hfmBenefit);
		    			vfmMaster.add(vfmBenefit);
		    			
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
		    		}
		    		
		    		// klaim
		    		if (intCekKlaim == 1) {
		    			HorizontalFieldManager hfmKlaim = cr.getHeaderRibbon("DAFTAR KLAIM", 0);
	        		    vfmMaster.add(hfmKlaim);
		    			
	        		    HorizontalFieldManager hfmColorKlaim = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                graphics.setBackgroundColor(Integer.parseInt("E0FFFF", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmColorKlaim.add(gfmKlaim);
		    			vfmMaster.add(hfmColorKlaim);
		    			
		    			HorizontalFieldManager hfmColorKlaim0 = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                graphics.setBackgroundColor(Integer.parseInt("E0FFFF", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmColorKlaim0.add(vfmDiagnosis);
		    			vfmMaster.add(hfmColorKlaim0);
		    			
		    			HorizontalFieldManager hfmColorKlaim1 = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                graphics.setBackgroundColor(Integer.parseInt("E0FFFF", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmColorKlaim1.add(gfmKlaim1);
		    			vfmMaster.add(hfmColorKlaim1);
		    			
		    			LabelField lblRecord = new LabelField(" " + strRecNo + "/" + strMaxNo + " ");
	        	        HorizontalFieldManager btPrevNext = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
	        	            public void paint(Graphics graphics){
	        	                graphics.setBackgroundColor(Integer.parseInt("b3b3b3", 16));
	        	                graphics.clear();
	        	                super.paint(graphics);
	        	            }
	        	        };
	        	        btPrevNext.setFont(cr.getCustomFont());
	        	        btPrevNext.add(btPrevKlaim);
	        	        btPrevNext.add(lblRecord);
		    			btPrevNext.add(btNextKlaim);
		    			vfmMaster.add(btPrevNext);
		    			
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
//		    			vfmMaster.add(new NullField(NullField.FOCUSABLE));
		    		}
				}
				catch (Exception exp) {
					// nothing
				}
			}
		} else {
			synchronized (UiApplication.getEventLock()) {
				Dialog.alert("Unknown content type: " + contentType);
			}
		}
	}
	
	/**
	 * membaca result berupa xml 
	 */
	private void displayNode( Node node, int depth ) {
		// ==> element artinya diapit oleh xml-tag, misal <nama>novan</nama>
        if ( node.getNodeType() == Node.ELEMENT_NODE ) {
            
        	StringBuffer buffer = new StringBuffer();
            indentStringBuffer( buffer, depth );
        	NodeList childNodes = node.getChildNodes();
            int numChildren = childNodes.getLength();
            Node firstChild = childNodes.item( 0 );
            
            // ==> 1 child artinya satu sub-element
            // bagian ini berusaha mencari text di antara element
            // bila menemukan element baru / sub-element, maka dilihat bagian dalamnya.. rekursif
            // bila menemukan text, akan ditampilkan
            if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) {
            	
            	if (strRootName.equalsIgnoreCase("Peserta")) {
            		
	            	// bandingkan dengan xml-nya
	        		if (node.getNodeName().equalsIgnoreCase("NoKartu")) {
	            		strHidNoKartu = firstChild.getNodeValue();
	            		gfmOrig.add(new LabelField("No Kartu"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(strHidNoKartu), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("NoPolis")) {
	            		strHidNoPolis = firstChild.getNodeValue();
	            		gfmOrig.add(new LabelField("No Polis"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(strHidNoPolis), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("NoPeserta")) {
	            		strHidNoReg = firstChild.getNodeValue();
	            		gfmOrig.add(new LabelField("No Peserta"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(strHidNoReg), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("TglLahir")) {
	            		strHidTglLahir = cf.ddMMMyyyy2ddMMyyyy(firstChild.getNodeValue());
	            		gfmOrig.add(new LabelField("Tgl Lahir"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("NamaPeserta")) {
	            		gfmOrig.add(new LabelField("Nama Peserta"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("Umur")) {
	            		gfmOrig.add(new LabelField("Umur"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(firstChild.getNodeValue() + " Tahun"), Field.FIELD_LEFT);
	            	} else if (node.getNodeName().equalsIgnoreCase("NamaPerusahaan")) {
	            		gfmOrig.add(new LabelField("Nama Perusahaan"), Field.FIELD_LEFT);
		            	gfmOrig.add(new LabelField(" : "), Field.FIELD_HCENTER);
		            	gfmOrig.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
	            	}
	            	
            	} else if (strRootName.equalsIgnoreCase("Plan")) {
            		
            		if (node.getNodeName().equalsIgnoreCase("KodePlan")) {
            			intCekPlan = 1;
            			//origBtPlan = new CustomButton(firstChild.getNodeValue(), Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.GREEN, 0);
            			//origBtPlan.setChangeListener(myListener);
            			
            			//origBtPlan = new RowButton(firstChild.getNodeValue(), Color.BLACK, Integer.parseInt("F5F5DC", 16), Color.BLACK, Integer.parseInt("FFE4C4", 16), RowButton.USE_ALL_WIDTH);
            			origBtPlan = new RowButton(firstChild.getNodeValue(), Color.BLACK, Integer.parseInt("FFE4C4", 16), Color.YELLOW, Color.RED, RowButton.USE_ALL_WIDTH);
            			origBtPlan.setFont(cr.getCustomFont());
            			origBtPlan.setChangeListener(myRowListener);
            			
            			HorizontalFieldManager hfmKodePlan = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                //graphics.setBackgroundColor(Integer.parseInt("b3b3b3", 16));
            	            	graphics.setBackgroundColor(Integer.parseInt("F5F5DC", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmKodePlan.setFont(cr.getCustomFont());
            	        hfmKodePlan.add(origBtPlan);
            	        vfm.add(hfmKodePlan);
//            			intIdxPlan = intIdxPlan + 1;
            		} else if (node.getNodeName().equalsIgnoreCase("Premi")) {
            			HorizontalFieldManager hfmKodePlan = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                //graphics.setBackgroundColor(Integer.parseInt("F0FFFF", 16));
            	            	graphics.setBackgroundColor(Integer.parseInt("F5F5DC", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmKodePlan.setFont(cr.getCustomFont());
            	        hfmKodePlan.add(new LabelField("   Premi: Rp " + firstChild.getNodeValue()));
            			vfm.add(hfmKodePlan);
            		} else if (node.getNodeName().equalsIgnoreCase("JnsJaminan")) {
            			HorizontalFieldManager hfmKodePlan = new HorizontalFieldManager(Field.USE_ALL_WIDTH){
            	            public void paint(Graphics graphics){
            	                //graphics.setBackgroundColor(Integer.parseInt("F0FFFF", 16));
            	            	graphics.setBackgroundColor(Integer.parseInt("F5F5DC", 16));
            	                graphics.clear();
            	                super.paint(graphics);
            	            }
            	        };
            	        hfmKodePlan.setFont(cr.getCustomFont());
            	        hfmKodePlan.add(new LabelField("   " + firstChild.getNodeValue()));
            	        vfm.add(hfmKodePlan);
            		}
            		
            	} else if (strRootName.equalsIgnoreCase("Benefit")) {
            		
           			hfmBenefit = new HorizontalFieldManager(Field.FIELD_LEFT);
        			final int intCekNoBenefit = intBenefitNo % 2;
            		HorizontalFieldManager hfmColorBenefit = new HorizontalFieldManager(Field.USE_ALL_WIDTH | Field.FOCUSABLE) {
            			public void paint(Graphics graphics){
            				if (intCekNoBenefit == 0) {
            					graphics.setBackgroundColor(Integer.parseInt("F5F5DC", 16));
            				} else {
            					graphics.setBackgroundColor(Integer.parseInt("FFE4C4", 16)); // pertama
            				}
                            graphics.clear();
                            super.paint(graphics);
                        }
            		};
            		hfmColorBenefit.setFont(cr.getCustomFont());
            		
            		if (node.getNodeName().equalsIgnoreCase("BenefitName")) {
            			intCekBenefit = 1;
            			hfmBenefit.add(new LabelField(intBenefitNo + ". ", Field.FOCUSABLE));
            			hfmBenefit.add(new LabelField(firstChild.getNodeValue()));
            			hfmColorBenefit.add(hfmBenefit);
            			vfmBenefit.add(hfmColorBenefit);
            		} else if (node.getNodeName().equalsIgnoreCase("BenefitLimit")) {
            			if (intBenefitNo < 10) {
            				hfmColorBenefit.add(new LabelField("     Limit: Rp " + firstChild.getNodeValue()));
            			} else {
            				hfmColorBenefit.add(new LabelField("       Limit: Rp " + firstChild.getNodeValue()));
            			}
            			vfmBenefit.add(hfmColorBenefit);
            			intBenefitNo = intBenefitNo + 1;
            		} else if (node.getNodeName().equalsIgnoreCase("PlanName")) {
            			strPlanName = firstChild.getNodeValue();
            		}
            		
            	} else if (strRootName.equalsIgnoreCase("Klaim")) {
            		
            		if (node.getNodeName().equalsIgnoreCase("RecNo")) {
            			intCekKlaim = 1;
            			strRecNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("MaxNo")) {
            			strMaxNo = firstChild.getNodeValue();
            		} else if (node.getNodeName().equalsIgnoreCase("KlaimNo")) {
            			gfmKlaim.add(new LabelField("No Klaim"), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("RegisterDate")) {
            			gfmKlaim.add(new LabelField("Tgl Register"), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("DateOfLoss")) {
            			gfmKlaim.add(new LabelField("Tgl Kejadian"), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("Location")) {
            			gfmKlaim.add(new LabelField("Tempat"), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField("Diagnosa"), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim.add(new LabelField(" "), Field.FIELD_LEFT);
            		} 
            		
            		if (node.getNodeName().equalsIgnoreCase("Diagnosa")) {
            			vfmDiagnosis.add(new LabelField(firstChild.getNodeValue()));
            			vfmDiagnosis.add(new LabelField(" "));
            		}  
            			
            		if (node.getNodeName().equalsIgnoreCase("TotProposed")) {
            			gfmKlaim1.add(new LabelField("Biaya Diajukan"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : Rp "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("TotAccepted")) {
            			gfmKlaim1.add(new LabelField("Biaya Dibayar"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : Rp "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("StatusKlaim")) {
            			gfmKlaim1.add(new LabelField("Status Klaim"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("TglBayar")) {
            			gfmKlaim1.add(new LabelField("Tgl Bayar"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("TglLunas")) {
            			gfmKlaim1.add(new LabelField("Tgl Lunas"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("StsProvider")) {
            			gfmKlaim1.add(new LabelField("Status Provider"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("TotalEkses")) {
            			gfmKlaim1.add(new LabelField("Total Ekses"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : Rp "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		} else if (node.getNodeName().equalsIgnoreCase("TglLunasEkses")) {
            			gfmKlaim1.add(new LabelField("Tgl Lunas Ekses"), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(" : "), Field.FIELD_LEFT);
            			gfmKlaim1.add(new LabelField(firstChild.getNodeValue()), Field.FIELD_LEFT);
            		}
            		
            		btPrevKlaim = new CustomButton(" << ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
            		btPrevKlaim.setFont(cr.getCustomFont());
            		btPrevKlaim.setChangeListener(prevListener);
            		
            		btNextKlaim = new CustomButton(" >> ", Color.BLACK, Color.LIGHTGRAY, Color.YELLOW, Color.RED, 0);
            		btNextKlaim.setFont(cr.getCustomFont());
            		btNextKlaim.setChangeListener(nextListener);
            	}
            	
            } else if (numChildren > 1) {
            	
                // ==> lebih dari 1 children artinya ada lebih dari satu sub-element
                //buffer.append( node.getNodeName() );
                //add( new RichTextField( buffer.toString() ) ); // dimatikan supaya tidak menuliskan nama node -nya
            	strRootName = node.getNodeName();
            	
            	// Recursively visit all this node's children.
                // ==> rekursif, panggil diri sendiri untuk menampilkan isinya
                for ( int i = 0; i < numChildren; ++i ) {
                    displayNode( childNodes.item( i ), depth + 1 );
                }
                
            } else if (numChildren == 0)  {
            	for ( int i = 0; i < numChildren; ++i ) {
                    displayNode( childNodes.item( i ), depth + 1 );
                }
            }
            
        } else {
        	
            // ==> bukan element, artinya tidak diapit oleh xml-tag.. berarti ini text-node
            // ==> pada aplikasi ini, tidak akan ada text-node.. karena semua akan memakai tag..
        	// ==> jadi sebetulnya bagian ini bisa dihapus..
            String nodeValue = node.getNodeValue();
            if ( nodeValue.trim().length() != 0 )  {
                StringBuffer buffer = new StringBuffer();
                indentStringBuffer( buffer, depth );
                buffer.append( '"' ).append( nodeValue ).append( '"' );
                vfmMaster.add(new LabelField(buffer.toString(), Field.USE_ALL_WIDTH));
            }
            
        }
    }
    
	/**
	 * Untuk Keperluan baca XML 
	 */
    private static void indentStringBuffer( StringBuffer buffer, int depth ) {
        int indent = depth * _tab;
        for ( int i = 0; i < indent; ++i ) {
            buffer.append( ' ' );
        }
    }
   
   /**
    * Action Listener Plan Name 
    */
   FieldChangeListener myRowListener = new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
	    	RowButton buttonField = (RowButton) field;
            fnViewBenefitScreen(buttonField.getLabel());
        }
   };
	
   /**
    * View Benefit
    */
   private void fnViewBenefitScreen(String strPlanName) {
	   CallHIDServlet chs = new CallHIDServlet();
	   chs.fnCallHIDServlet3(this, strHidNoKartu, strHidNoPolis, strHidNoReg, strHidTglLahir, strPlanName);
   }
	
    /**
     * Action Listener btPrev
     */
    FieldChangeListener prevListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 int intNewNum = Integer.parseInt(strRecNo) - 1;
	    	 int intMaxNo = Integer.parseInt(strMaxNo);
	    	 if (intNewNum < 1) {
	    		 intNewNum = 1;
	    	 }
             strRecNo = String.valueOf(intNewNum);
             strMaxNo = String.valueOf(intMaxNo);
             fnViewKlaimScreen();
         }
    };
    
    /**
     * Action Listener btNext
     */
    FieldChangeListener nextListener = new FieldChangeListener() {
	     public void fieldChanged(Field field, int context) {
	    	 int intNewNum = Integer.parseInt(strRecNo) + 1;
	    	 int intMaxNo = Integer.parseInt(strMaxNo);
	    	 if (intNewNum > intMaxNo) {
	    		 intNewNum = intMaxNo;
	    	 }
             strRecNo = String.valueOf(intNewNum);
             strMaxNo = String.valueOf(intMaxNo);
             fnViewKlaimScreen();
             
         }
    };
}
